<?php
     header("content-type:text/html;charset=UTF-8"); 
     include_once("WxPayPubHelper.php");
	
	 error_log("=============wxpay=====================",0);
	 $token = $_GET['token'];
	 error_log("token===========".$token,0);
	 $openid = $_GET['wecha_id'];
	 error_log("openid=============".$openid,0);
	 $outTradeNo = $_GET['order_sn'];
	 error_log("outTradeNo===========".$outTradeNo,0);
	 $sub = "专家咨询微信支付";
	 error_log("sub=============".$sub,0);
	 $fee = $_GET['total_fee'];
	 error_log("fee============".$fee,0);
	 $totalFee= floatval($fee);
	 $total_fee = floatval($fee) * 100; 
	 
	 
	 $appid = $_GET['appid'];
	 error_log("appid=============".$appid,0);
	 $mchid = $_GET['mchid'];
	 error_log("mchid============".$mchid,0);
	 $key= $_GET['key'];
	 error_log("key============".$key,0);
	 $appsecret = $_GET['appsecret'];
	 error_log("appsecret============".$appsecret,0);
	 $group_id = $_GET['group_id'];
	 error_log("group_id============".$group_id,0);
	 $site_url = $_GET['site_url'];
     error_log("site_url============".$site_url,0);
	 
	 
	 
	 $cmname="DDL";
	 $companyName=iconv("GBK","UTF-8",$cmname);
	 
	 $tl="WXPAY";
	 $title = iconv("GBK","UTF-8",$tl);
	 
	 $fh1="$";
	 $fh=iconv("GBK","UTF-8",$fh1);
	 
	 $skf1="RECEIVE:";
	 $skf=iconv("GBK","UTF-8",$skf1);
	 
	 $pt1="PRODUCT:";
	 $pt=iconv("GBK","UTF-8",$pt1);
	 
	 $zf1="PAYN";
	 $zf=iconv("GBK","UTF-8",$zf1);
	 
	 	
	 //使用jsapi接口
	 $jsApi = new JsApi_pub();

     //=========步骤2：使用统一支付接口，获取prepay_id============
	 //使用统一支付接口
	 $unifiedOrder = new UnifiedOrder_pub();
	
	 $unifiedOrder->setParameter("openid","$openid");//��Ʒ����
	 $unifiedOrder->setParameter("body","$sub");//商品描述

	 $unifiedOrder->setParameter("out_trade_no","$outTradeNo");//商户订单号 
	 $unifiedOrder->setParameter("total_fee","$total_fee");//总金额
	 //需要绝对路径，外网可访问，不支持非80端口，同时注意不要被防火墙拦截；可自行用fiddler模拟post访问是否正常。
	 $unifiedOrder->setParameter("notify_url","http://".$_SERVER['HTTP_HOST']."/api/wxpay/notify_url.php");//通知地址 
	 $unifiedOrder->setParameter("trade_type","JSAPI");//交易类型


	// $prepay_id = $unifiedOrder->getPrepayId();
	/* begin add by hutingting 微信支付多用户处理  2015-06-18 */
	 $prepay_id = $unifiedOrder->getPrepayId($appid,$mchid,$key,$appsecret);
	 error_log("wxpay.php============prepay_id=".$prepay_id,0);
	 if($prepay_id == "ORDERPAID")
	 {
	          // 已经支付，再次申请支付的处理
		      header("Location:http://tech.wawame.com/index.php?g=Wap&m=Userinfo&a=consultations&token=qkjedf1477883218");
	 }
     /* end add by hutingting 微信支付多用户处理  2015-06-18 */
	 //=========步骤3：使用jsapi调起支付============
	 $jsApi->setPrepayId($prepay_id);

	 $jsApiParameters = $jsApi->getParameters($appid,$key);

?>
<html>
<head>
		<meta http-equiv="content-type" content="text/html;charset=UTF-8">
		<title><?php echo $token;echo " ";echo $openid; echo " ";echo $site_url; echo " "; echo $outTradeNo; echo " "; echo $group_id?></title>
</head>
<script language="javascript">
        //����΢��JS api ֧��
		function jsApiCall()
		{
			WeixinJSBridge.invoke(
				'getBrandWCPayRequest',
				<?php echo $jsApiParameters; ?>,
				function(res){
				WeixinJSBridge.log(res.err_msg);
				var title = document.title;
		   		var arr   = title.split(" ");
		   		var token = arr[0];
		  		var wecha_id = arr[1];	
				var site_url = arr[2];
				var order_sn = arr[3];
				var group_id = arr[4];

				if(res.err_msg == "get_brand_wcpay_request:cancel" || res.err_msg == "get_brand_wcpay_request:fail")
				{
					// 支付失败 或取消支付的处理
					window.location.href="http://tech.wawame.com/index.php?g=Wap&m=Userinfo&a=consultations&token=qkjedf1477883218";
				}
				else if(res.err_msg == "get_brand_wcpay_request:ok")
			    {		
					// 支付成功的处理
					   window.location.href="http://tech.wawame.com/index.php?g=Wap&m=Userinfo&a=consultations&token=qkjedf1477883218";
			    }
			    else
				{
                       //返回跳转到订单详情页面
                       alert(支付失败);
                       window.location.href="http://tech.wawame.com/index.php?g=Wap&m=Userinfo&a=consultations&token=qkjedf1477883218";
                 }
				}
			);
		}

	    window.onload =	function()
		{
			if (typeof WeixinJSBridge == "undefined"){
			    if( document.addEventListener ){
			        document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
			    }else if (document.attachEvent){
			        document.attachEvent('WeixinJSBridgeReady', jsApiCall); 
			        document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
			    }
			}else{
			    jsApiCall();
			}
		}
		
</script>
<body>
<!--<div style="text-align:center;padding-top:60px;">
<font size="60px" face="SimSun"><?php echo $sub; ?></font> <br/><br/><br/>
<font size="60px" face="SimSun"><?php echo $fh;echo $totalFee; ?></font><br/><br/><br/>
<hr align=center width="70%" SIZE=3><br/><br/><br/>
<font size="40px" face="SimSun"><?php echo $skf;?>&nbsp;&nbsp;&nbsp;   <?php echo $companyName ?></font><br/><br/><br/> 
<font size="40px" face="SimSun"><?php echo $pt;?>&nbsp;&nbsp;&nbsp;    <?php echo $sub ?></font><br/><br/><br/>
<hr align=center width="70%" SIZE=3><br/><br/><br/><br/><br/><br/>
<button type="button" onclick="callpay()" style="width:400px; text-align:center;font-size:60px;background:-webkit-gradient(linear, 0% 0%, 0% 100%, from(#9C0), to(#9C3));line-height: 80px;padding-top:60px;"><?php echo $zf; ?></button>
</div> -->
</body>
</html>